<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            .flex  {
                display: flex!important;
                flex-direction: row;
                margin-bottom: 10px;
            }
        </style>
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">

<!--                    <div class="layui-form-item">-->
<!--                        <label class="layui-form-label">题库分类</label>-->
<!--                        <div class="layui-input-block">-->
<!--                            <div name="category_id" id="category_id"></div>-->
<!--                        </div>-->
<!--                    </div>-->

                    <div class="layui-form-item">
                        <label class="layui-form-label">题目类型</label>
                        <div class="layui-input-block">
                            <input type="radio" name="type" class="layui-radio" value="1" title="单选" checked>
                            <input type="radio" name="type" class="layui-radio" value="2" title="多选">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">题目</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">选项</label>
                        <div class="layui-input-block" id="options">
                            <div class="layui-input-inline flex">
                                <input type="text" name="options[]" value="<?=$options[0]?>" style="width: 200px;" class="layui-input">
                                <input type="checkbox" name="answer[]" value="1" title="是否答案" <?php if (in_array(1, $answers)): ?> checked <?php endif ?>>

                                <button type="button" class="layui-btn layui-btn-normal add">+</button>
                            </div>

                            <?php foreach ($options as $key=>$option) :?>
                                <?php if ($key > 0) :?>
                                <div class="layui-input-inline flex template">
                                    <input type="text" name="options[]" value="<?=$option?>" style="width: 200px; display: inline-block;" class="layui-input">
                                    <input type="checkbox" name="answer[]" title="是否答案" value="<?=$key+1?>" <?php if (in_array($key+1, $answers)): ?> checked <?php endif ?>>

                                    <button type="button" class="layui-btn layui-btn-normal remove">-</button>
                                </div>
                                <?php endif ?>
                            <?php endforeach ?>

                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <input type="hidden" name="category_id" value="1" class="layui-input">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <div class="layui-input-inline flex template" style="display: none!important;">
            <input type="text" name="options[]" value="" style="width: 200px; display: inline-block;" class="layui-input">
            <input type="checkbox" name="answer[]" title="是否答案" value="1">

            <button type="button" class="layui-btn layui-btn-normal remove">-</button>
        </div>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/question-store/select" + location.search;
            const UPDATE_API = "/app/admin/question-store/update";

            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                let $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                if (key === 'type') {
                                    obj.eq(value-1).prop('checked', true);
                                    return;
                                }
                                obj.attr("value", value);
                            }


                        });

                        layui.use(["jquery", "xmSelect"], function() {
                            layui.$.ajax({
                                url: "/app/admin/question-category/get",
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#category_id").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#category_id",
                                        name: "category_id",
                                        initValue: initValue,
                                        data: res.data,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                    });
                                    if (res.code) {
                                        return layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });
                        
                        
                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                        layui.form.render();
                    }
                });
            });
            let $ = layui.$;
            $(function() {
                $('.add').click(function() {
                    $('.template:last').clone().show().appendTo('#options');
                    rebuildValue()
                    layui.form.render();
                })

                $(document).on('click', '.remove', function(data) {
                    console.log($(this).parent())
                    $(this).parent().remove();
                    rebuildValue()
                    layui.form.render();

                })
            });

            function rebuildValue()
            {
                $("#options input[name='answer[]']").each(function(key) {
                    $(this).val(key+1)
                })
            }



            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
